//package com.logistics.relay.service;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.logistics.delivery.service.DeliveryService;
//import com.logistics.erp.entity.domain.ErpInvbill;
//import com.logistics.erp.service.ErpInvBillService;
//import com.logistics.inventory.entity.domain.Inventory;
//import com.logistics.inventory.entity.domain.InventoryDetail;
//import com.logistics.inventory.service.InventoryDetailService;
//import com.logistics.inventory.service.InventoryService;
//import com.logistics.map.entity.domain.MapComponents;
//import com.logistics.map.service.MapComponentsService;
//import com.logistics.pick.entity.domain.Pick;
//import com.logistics.qrcode.service.QrcodeService;
//import com.logistics.relay.entity.RelayDelivery;
//import com.logistics.relay.entity.StationMaterial;
//import com.logistics.relay.mapper.RelayPickMapper;
//import com.logistics.utils.constants.Constants;
//import com.logistics.utils.exception.MessageException;
//import com.logistics.utils.tool.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.List;
//
//@Service
//public class ArtificialRelayService {
//
//    @Resource
//    private RelayPickMapper relayPickMapper;
//
//    @Autowired
//    private QrcodeService qrcodeService;
//
//    @Autowired
//    private MapComponentsService mapComponentsService;
//
//    @Autowired
//    private InventoryService inventoryService;
//
//    @Autowired
//    private InventoryDetailService inventoryDetailService;
//
//    @Autowired
//    private ErpInvBillService erpInvBillService;
//
//    @Autowired
//    private DeliveryService deliveryService;
//
//    public List<StationMaterial> getStationInfo(String code) throws Exception {
//
//        String station = qrcodeService.valueByCode(code, Constants.CODE_TYPE_LOCATION);
//        String wcCode = "";
//        String wcName = "";
//        List<StationMaterial> stationMaterialList = new ArrayList<>();
//
//        //校验站点码是否存在
//        if (StringUtils.isNull(station)) {
//            throw new MessageException("站点码不存在");
//        }
//
//        //校验是否已经创建配送任务
//        MapComponents mapComponents = mapComponentsService.getOne(new LambdaQueryWrapper<MapComponents>()
//                .eq(MapComponents :: getLocation, station)
//                .eq(MapComponents :: getLocationLock, Constants.LOCATION_LOCK));
//        if (StringUtils.isNotNull(mapComponents)) {
//            throw new MessageException("该站点已经存在配送任务，请勿重复创建");
//        }
//
//        //获取站点托盘信息
//        Inventory inventory = inventoryService.getOne(new LambdaQueryWrapper<Inventory>()
//                        .eq(Inventory :: getStation, station));
//
//        if (StringUtils.isNotNull(inventory)) {
//            //获取站点物料信息
//            List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(
//                    new LambdaQueryWrapper<InventoryDetail>()
//                            .eq(InventoryDetail :: getContainer, inventory.getContainer()));
//
////            //获取ERP领料单目的地 - 班组
////            PickContainer pickContainer = pickContainerService.getOne(new LambdaQueryWrapper<PickContainer>()
////                    .eq(PickContainer :: getStation, station).eq(PickContainer :: getContainer, inventory.getContainer())
////                    .eq(PickContainer :: getStatus, Constants.PICK_STATE_SUCCESS));
////
////            //获取班组名称
////            Organization organization = organizationService.getOne(new LambdaQueryWrapper<Organization>()
////                    .eq(Organization :: getCode, pickContainer == null ? "" : pickContainer.getWcCode()));
//
//            //获取拣配单
//            Pick pick = relayPickMapper.findPick(inventory.getContainer(), station);
//            if (StringUtils.isNull(pick)) {
//                throw new MessageException("该站点没有拣配单");
//            }
//
//            ErpInvbill erpInvbill = erpInvBillService.getOne(new LambdaQueryWrapper<ErpInvbill>()
//                    .eq(ErpInvbill::getErpInvbillCode, pick.getErpInvbillCode()));
//
//            if (StringUtils.isNull(erpInvbill.getEndLocation())) {
//                wcName = "无班组信息";
//                wcCode = "";
//
//            } else {
//                wcName = erpInvbill.getEndLocationName();
//                wcCode = erpInvbill.getEndLocation();
//            }
//
//            for (InventoryDetail item : inventoryDetailList) {
//                stationMaterialList.add(StationMaterial.build(inventory.getContainer(), item.getItemCode(),
//                        item.getItemName(), item.getItemQty().toString(), wcName, wcCode));
//            }
//        }
//
//        return stationMaterialList;
//    }
//
//    public void createDelivery(RelayDelivery relayDelivery) throws Exception {
//        String station = qrcodeService.valueByCode(relayDelivery.getStartStation(), Constants.CODE_TYPE_LOCATION);
//
//        //校验是否已经创建配送任务
//        MapComponents mapComponents = mapComponentsService.getOne(new LambdaQueryWrapper<MapComponents>()
//                .eq(MapComponents :: getLocation, relayDelivery.getStartStation())
//                .eq(MapComponents :: getLocationLock, Constants.LOCATION_LOCK));
//        if (StringUtils.isNotNull(mapComponents)) {
//            throw new MessageException("该站点已经存在配送任务，请勿重复创建");
//        }
//
//        //获取拣配单
//        Pick pick = relayPickMapper.findPick(relayDelivery.getContainer(), station);
//        if (StringUtils.isNull(pick)) {
//            throw new MessageException("该站点没有拣配单");
//        }
//
//        //根据拣配单班组获取目的地站点
////        List<String> inventoryList = inventoryService.list(new LambdaQueryWrapper<Inventory>()
////                .isNotNull(Inventory::getStation)).stream().map(Inventory::getStation).collect(Collectors.toList());
////
////        MapComponents mapComponent = mapComponentsService.getOne(new LambdaQueryWrapper<MapComponents>()
////                .eq(MapComponents::getClazz, Constants.COMPONENTS_TYPE_LOCATION)
////                .eq(MapComponents::getLocationWcCode, relayDelivery.getWcCode())
////                .eq(MapComponents::getLocationLock, Constants.LOCATION_RELEASE)
////                .notIn(StringUtils.isNotNull(inventoryList) && inventoryList.size() > 0,
////                        MapComponents::getLocation, inventoryList)
////                .orderByAsc(MapComponents::getLocation)
////                .last("limit 1"));
////        //没有空闲站点
////        if (StringUtils.isNull(mapComponent)) {
////            throw new MessageException("没有可配送的空闲目的站点");
////        }
////
////        DeliverySave deliverySave = DeliverySave.build("1", "", station, mapComponent.getLocation(),
////                pick.getId().toString(), "0", relayDelivery.getContainer());
////
////        deliveryService.save(deliverySave);
//
//        deliveryService.createDelivery(pick.getId().toString());
//    }
//}
